package com.school.controller.admin;

import com.school.service.UsersService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/admin")
public class UsersController {
    @Resource
    private UsersService usersService;
    @RequestMapping("/users")
    public String usersPage(HttpServletRequest  request) {
        request.setAttribute("path", "tea_shop_user");
        return "admin/tea_shop_user";
    }
    @GetMapping("/users/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        PageResult pageResult = usersService.getUsersPage(pageQueryUtil);
        return ResultGenerator.genSuccessResult(pageResult);
    }
}
